var widget = {};
widget = {
    _UIDEFAULFS: {
        currentSms: null
    },
    _rebuildUiDefaults: function (options) {
        widget._UIDEFAULFS = Iptools.Tool._extend(widget._UIDEFAULFS, options);
    },
    _addEventListener: function (options) {
        $(options.container).on(options.type, options.target, options.event);
    },
    _bindingDomEvent: function () {
        widget._enableVerifyModal();
        widget._enableCompleteModal();
    },
    _bindingEventAfterLoad: function () {
    },
    _init: function () {
        widget._bindingDomEvent();
        widget._bindValidation();
        widget._setSmsList();
    },
    _bindValidation: function () {
        $("#verify_form").bootstrapValidator({
            fields: {
                regExp: {
                    validators: {
                        notEmpty: {
                            message: "请输入正则验证表达式"
                        }
                    }
                }
            }
        });
    },
    _setSmsList: function () {
        $("#sms_verify_list").loading();
        $("#sms_complete_list").loading();
        Iptools.GetJson({
            url: "system/getSmsTemplateList",
            data: {
                token: Iptools.DEFAULTS.token
            }
        }).done(function (r) {
            if (r && r.verify && r.verify.length) {
                $("#sms_verify_list").empty();
                for (var i = 0; i < r.verify.length; i++) {
                    $("#sms_verify_list").append(widget._setSmsVerifyBlock(r.verify[i]));
                }
            } else {
                $("#sms_verify_list").html("<div class=\"sms-empty-hint\">暂无待审核模板</div>");
            }
            if (r && r.complete && r.complete.length) {
                $("#sms_complete_list").empty();
                for (var i = 0; i < r.complete.length; i++) {
                    $("#sms_complete_list").append(widget._setSmsCompleteBlock(r.complete[i]));
                }
            } else {
                $("#sms_complete_list").html("<div class=\"sms-empty-hint\">暂无待审核模板</div>");
            }
        }).fail(function () {
            $("#sms_verify_list").html("<div class=\"sms-empty-hint\">暂无待审核模板</div>");
            $("#sms_complete_list").html("<div class=\"sms-empty-hint\">暂无待审核模板</div>");
        });
    },
    _setSmsVerifyBlock: function (data) {
        var dp = document.createElement("div"), db = document.createElement("div"),
            table = document.createElement("table"), button = document.createElement("button");
        $(dp).addClass("sms-panel col-lg-3 col-md-4 col-sm-6 col-xs-12");
        $(db).addClass("sms-block").append("<div class=\"sms-header\"><span class=\"fa fa-envelope-o\"></span></div>");
        $(table).append("<tr><td class=\"info-name\">租户ID</td><td class=\"info-text\">" + data.tenant_id + "</td></tr>",
            "<tr><td class=\"info-name\">租户信息</td><td class=\"info-text\">" + data.company_info + "</td></tr>",
            "<tr><td class=\"info-name\">模板名称</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.name) + "</td></tr>",
            "<tr><td class=\"info-name\">创建时间</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.create_time) + "</td></tr>",
            "<tr><td class=\"info-name\">模板内容</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.content) + "</td></tr>");
        $(db).append($("<div class=\"sms-content i-scroll\"></div>").append(table));
        $(button).addClass("btn commonBtn").attr({
            "type": "button",
            "data-toggle": "modal",
            "data-target": "#sms_verify_modal"
        }).data("data", data).html("审核");
        $(db).append($("<div class=\"sms-footer\"></div>").append(button));
        return $(dp).append(db);
    },
    _setSmsCompleteBlock: function (data) {
        var dp = document.createElement("div"), db = document.createElement("div"),
            table = document.createElement("table"), button = document.createElement("button");
        $(dp).addClass("sms-panel col-lg-3 col-md-4 col-sm-6 col-xs-12");
        $(db).addClass("sms-block success").append("<div class=\"sms-header\"><span class=\"fa fa-envelope-o\"></span></div>");
        $(table).append("<tr><td class=\"info-name\">租户ID</td><td class=\"info-text\">" + data.tenant_id + "</td></tr>",
            "<tr><td class=\"info-name\">租户信息</td><td class=\"info-text\">" + data.company_info + "</td></tr>",
            "<tr><td class=\"info-name\">模板名称</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.name) + "</td></tr>",
            "<tr><td class=\"info-name\">创建时间</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.create_time) + "</td></tr>",
            "<tr><td class=\"info-name\">模板内容</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.content) + "</td></tr>",
            "<tr><td class=\"info-name\">正则验证</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.reg_express) + "</td></tr>",
            "<tr><td class=\"info-name\">键值对</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.key_value) + "</td></tr>",
            "<tr><td class=\"info-name\">NOTE</td><td class=\"info-text\">" + Iptools.Tool._GetProperValue(data.note) + "</td></tr>");
        $(db).append($("<div class=\"sms-content i-scroll\"></div>").append(table));
        $(button).addClass("btn cancel").attr({
            "type": "button",
            "data-toggle": "modal",
            "data-target": "#sms_complete_modal"
        }).data("data", data).html("撤销");
        $(db).append($("<div class=\"sms-footer\"></div>").append(button));
        return $(dp).append(db);
    },
    _enableVerifyModal: function () {
        widget._addEventListener({
            container: "body",
            target: "#sms_verify_modal",
            type: "show.bs.modal",
            event: function (e) {
                var btn = $(e.relatedTarget);
                var data = btn.data("data");
                if (data) {
                    widget._UIDEFAULFS.currentSms = data;
                    $("#verify_sms_tenant_id").html(data.tenant_id);
                    $("#verify_sms_tenant_info").html(data.company_info);
                    $("#verify_sms_name").html(data.name);
                    $("#verify_sms_create_time").html(data.create_time);
                    $("#verify_sms_content").html(data.content);
                } else {
                    return false;
                }
            }
        });
        widget._addEventListener({
            container: "body",
            target: "#sms_verify_modal",
            type: "hidden.bs.modal",
            event: function () {
                $("#sms_verify_modal").find(".sms-modal-text:not(.input)").empty();
                $("#sms_verify_modal").find("input").val("");
                $("#verify_form").data("bootstrapValidator").resetForm();
            }
        });
        widget._addEventListener({
            container: "body",
            target: "#verify_confirm",
            type: "click",
            event: function () {
                $("#verify_form").bootstrapValidator('validate');
                if ($("#verify_form").data("bootstrapValidator").isValid()) {
                    var me = $(this);
                    me.addClass("no-events").button("loading");
                    var data = {
                        token: Iptools.DEFAULTS.token,
                        tenantId: widget._UIDEFAULFS.currentSms.tenant_id,
                        id: widget._UIDEFAULFS.currentSms.id,
                        status: 2,
                        regExp: $("#verify_sms_regexp").val()
                    };
                    if ($("#verify_sms_key_value").val() != "") {
                        data["keyValue"] = $("#verify_sms_key_value").val();
                    }
                    if ($("#verify_sms_note").val() != "") {
                        data["note"] = $("#verify_sms_note").val();
                    }
                    Iptools.PutJson({
                        url: "system/updateSmsTemplate",
                        data: data
                    }).done(function () {
                        widget._setSmsList();
                        $("#sms_verify_modal").modal("hide");
                        me.removeClass("no-events").button("reset");
                    }).fail(function () {
                        Iptools.Tool.pAlert({
                            type: "danger",
                            title: "保存失败",
                            content: "系统异常"
                        });
                        me.removeClass("no-events").button("reset");
                    });
                }
            }
        });
    },
    _enableCompleteModal: function () {
        widget._addEventListener({
            container: "body",
            target: "#sms_complete_modal",
            type: "show.bs.modal",
            event: function (e) {
                var btn = $(e.relatedTarget);
                var data = btn.data("data");
                if (data) {
                    widget._UIDEFAULFS.currentSms = data;
                    $("#complete_tenant_id").html(data.tenant_id);
                    $("#complete_sms_tenant_info").html(data.company_info);
                    $("#complete_sms_name").html(data.name);
                    $("#complete_sms_create_time").html(data.create_time);
                    $("#complete_sms_content").html(data.content);
                    $("#complete_reg_exp").html(Iptools.Tool._GetProperValue(data.reg_express));
                    $("#complete_key_value").html(Iptools.Tool._GetProperValue(data.key_value));
                    $("#complete_note").html(Iptools.Tool._GetProperValue(data.note));
                } else {
                    return false;
                }
            }
        });
        widget._addEventListener({
            container: "body",
            target: "#sms_complete_modal",
            type: "hidden.bs.modal",
            event: function () {
                $("#sms_complete_modal").find(".sms-modal-text").empty();
            }
        });
        widget._addEventListener({
            container: "body",
            target: "#complete_cancel_confirm",
            type: "click",
            event: function () {
                if (confirm("确定撤销将此模板还原为未审核？")) {
                    var me = $(this);
                    me.addClass("no-events").button("loading");
                    var data = {
                        token: Iptools.DEFAULTS.token,
                        tenantId: widget._UIDEFAULFS.currentSms.tenant_id,
                        id: widget._UIDEFAULFS.currentSms.id,
                        status: 1
                    };
                    Iptools.PutJson({
                        url: "system/updateSmsTemplate",
                        data: data
                    }).done(function () {
                        widget._setSmsList();
                        $("#sms_complete_modal").modal("hide");
                        me.removeClass("no-events").button("reset");
                    }).fail(function () {
                        Iptools.Tool.pAlert({
                            type: "danger",
                            title: "保存失败",
                            content: "系统异常"
                        });
                        me.removeClass("no-events").button("reset");
                    });
                }
            }
        });
    }
};